package com.hrbxlh.virtualization.controller;

import com.hrbxlh.virtualization.param.ClusterAutoCreateParam;
import com.hrbxlh.virtualization.param.ClusterUpdateParam;
import com.hrbxlh.virtualization.result.Result;
import com.hrbxlh.virtualization.service.ClusterService;
import com.hrbxlh.virtualization.util.ObjectUtil;
import com.hrbxlh.virtualization.util.SessionUtil;
import com.hrbxlh.virtualization.vo.ClusterVO;
import com.hrbxlh.virtualization.vo.DesktopVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/cluster")
public class ClusterController {

    @Autowired
    private ClusterService clusterService;

    /**
     * 查询集群列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<List<ClusterVO>> listClusters() {
        List<ClusterVO> clusterList = clusterService.listClusters(SessionUtil.getStuorteaId());
        return Result.success(clusterList);
    }

    /**
     * 查询集群列表
     */
    @RequestMapping(value = "/listByAllocate", method = RequestMethod.GET)
    public Result<List<ClusterVO>> listClustersByAllocate() {
        List<ClusterVO> clusterList = clusterService.listClustersByAllocate(SessionUtil.getStuorteaId());
        return Result.success(clusterList);
    }

    /**
     * 创建集群
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result<Map<String, Object>> addCluster(@RequestBody @Valid ClusterAutoCreateParam clusterCreateParam) {
        Long clusterId = clusterService.autoCreateCluster(clusterCreateParam);
        return Result.success(ObjectUtil.build().addObject("clusterId", clusterId).map());
    }

    /**
     * 查看集群
     *
     * @param clusterId 集群Id
     */
    @RequestMapping(value = "/desktop", method = RequestMethod.GET)
    public Result<DesktopVO> getDesktopByCluster(Long clusterId) {
        DesktopVO desktopVO = clusterService.queryDesktop(SessionUtil.getStuorteaId(), clusterId);
        return Result.success(desktopVO);
    }

    /**
     * 查询集群详情
     *
     * @param id 集群Id
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Result<ClusterVO> queryCluster(Long id) {
        ClusterVO clusterVO = clusterService.queryCluster(id);
        return Result.success(clusterVO);
    }

    /**
     * 修改集群
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateCluster(@RequestBody @Valid ClusterUpdateParam clusterUpdateParam) {
        clusterService.updateCluster(clusterUpdateParam);
        return Result.success();
    }

    /**
     * 删除集群
     *
     * @param id       集群Id
     * @param isForced 是否强制删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result deleteCluster(Long id, @RequestParam(required = false, defaultValue = "false") Boolean isForced) {
        clusterService.deleteCluster(id, isForced, SessionUtil.getStuorteaId());
        return Result.success();
    }

}
